{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/81_goes_timelapse.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "Uncomment the following line to install [geemap](https://geemap.org) if needed.\n",
    "\n",
    "This notebook is adapted from Justin Braaten's [JavaScript example](https://code.earthengine.google.com/57245f2d3d04233765c42fb5ef19c1f4). Credits to Justin Braaten. See also his Medium post - [GOES in Earth Engine](https://jstnbraaten.medium.com/goes-in-earth-engine-53fbc8783c16)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "region = ee.Geometry.Polygon(\n",
    "    [\n",
    "        [\n",
    "            [-159.5954379282731, 60.40883060191719],\n",
    "            [-159.5954379282731, 24.517881970830725],\n",
    "            [-114.2438754282731, 24.517881970830725],\n",
    "            [-114.2438754282731, 60.40883060191719],\n",
    "        ]\n",
    "    ],\n",
    "    None,\n",
    "    False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2021-10-24T14:00:00\"\n",
    "end_date = \"2021-10-25T01:00:00\"\n",
    "data = \"GOES-17\"\n",
    "scan = \"full_disk\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "col = geemap.goes_timeseries(start_date, end_date, data, scan, region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "visParams = {\n",
    "    \"bands\": [\"CMI_C02\", \"CMI_GREEN\", \"CMI_C01\"],\n",
    "    \"min\": 0,\n",
    "    \"max\": 0.8,\n",
    "    \"dimensions\": 700,\n",
    "    \"framesPerSecond\": 9,\n",
    "    \"region\": region,\n",
    "    \"crs\": col.first().projection(),\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.expanduser(\"~/Downloads\")\n",
    "out_gif = os.path.join(out_dir, \"goes_timelapse.gif\")\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.download_ee_video(col, visParams, out_gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "timestamps = geemap.image_dates(col, date_format=\"YYYY-MM-dd HH:mm\").getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.add_text_to_gif(\n",
    "    out_gif,\n",
    "    out_gif,\n",
    "    xy=(\"3%\", \"3%\"),\n",
    "    text_sequence=timestamps,\n",
    "    font_size=20,\n",
    "    font_color=\"#ffffff\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "All the above steps can be reduced to just one line of code using the `goes_timelapse()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "timelapse = geemap.goes_timelapse(\n",
    "    region, out_gif, start_date, end_date, data, scan, framesPerSecond=10\n",
    ")\n",
    "geemap.show_image(timelapse)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
